package action.league;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import utility.Uploader;
import model.dao.LeagueModel;
import model.dto.League;
import controller.Action;
import controller.ActionForward;

public class ActionAddLeague implements Action{

	@Override
	public ActionForward execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		League dto=new League();
		LeagueModel dao=new LeagueModel();
		Uploader up=new Uploader(request, "upload"+File.separator+"league");
		int new_index=dao.getLastIndex()+1;
		boolean hasImageFile=up.upload( ""+new_index);
		dto.setCountryId(Integer.parseInt(up.getMutilPathRequest().getParameter("country_id")));
		dto.setName(up.getMutilPathRequest().getParameter("league_name"));
		
		if(hasImageFile){
			dto.setImage(up.getPreFix()+new_index+"."+up.getExtension());//name as sequence
		}else{
		dto.setImage("default.jpg");//old name
		}
		
		
		if(dao.insert(dto))
			System.out.println("Inserted succesfully.");
		else
			System.out.println("Fail inserting!!!");
		
		ActionForward forward=new ActionForward();
		forward.setPath("league.sp");
		forward.setRedirect(true);
		return forward;
	}

}
